import traceback

from selenium import webdriver
import os
import time
import re
from bs4 import BeautifulSoup
import random
import requests

from selenium.webdriver.common.by import By
import mysql_connect
from ShaneEssay import ShaneEssay
from ZhihuAnswer import ZhihuAnswer
from ZhihuUser import ZhihuUser
from SnowflakeGenerator import SnowflakeGenerator
from ShaneQuestion import ShaneQuestion

generator = SnowflakeGenerator(worker_id=1, datacenter_id=1)

answer_url = "https://www.zhihu.com/api/v4/answers/1307614528/root_comments?order=normal&limit=20&offset=0&status=open"

def get_question_id_list():
    conn, cursor = mysql_connect.get_conn()
    question_id_list = []
    try:

        sql = """
            SELECT
        id
    FROM
        shane_question
    WHERE
        plain_text = ''

        """
        cursor.execute(sql)
        result = cursor.fetchall()
        question_id_list = [essay_id[0] for essay_id in result]
    except:
        traceback.print_exc()
    finally:
        cursor.close()
        conn.close()
    return question_id_list


def extract_one_data(file_name: str):
    html_text = ""
    shane_question = ShaneQuestion()
    shane_question.id = int(file_name.split(".")[0])
    with open(f"other_files/question_title_htmls/question_htmls/{file_name}", "r", encoding="utf-8") as r_file:
        html_text = r_file.read()
    soup = BeautifulSoup(html_text, "html.parser")
    soup_title = soup.find("h1", {"class": "QuestionHeader-title"})
    shane_question.title = soup_title.text
    soup_content = soup.find("span", {"itemprop": "text"})
    shane_question.content = soup_content.text
    shane_question.plain_text = shane_question.content
    shane_question.display_text = shane_question.content[0:100]
    shane_question.zhihu_question_id = shane_question.id

    zhihu_user_list: list[ZhihuUser] = []
    zhihu_answer_list: list[ZhihuAnswer] = []
    # soup_users = soup.find_all("div",{"class": "AuthorInfo-content"})
    # for soup_user in soup_users :
    #     zhihu_user = ZhihuUser()
    #     print(soup_user)
    # soup_username = soup_user.find("meta",{"itemprop": "name"})
    # zhihu_user.username = soup_username["content"]
    # print(zhihu_user.username)
    soup_answers_div = soup.find("div", {"class": "css-0", "role": "list"})
    # print(soup_answers_div)
    soup_answer_list = soup_answers_div.find_all("div", {"class": "List-item", "tabindex": "0"})

    for soup_answer in soup_answer_list:
        soup_answer_url = soup_answer.find_all("meta", {"itemprop": "url"})[-1]
        answer_url = soup_answer_url.get("content")
        answer_id = int(answer_url.split("/")[-1])

        zhihu_user = ZhihuUser()
        zhihu_user.zhihu_answer_id = answer_id
        zhihu_user.zhihu_question_id = int(file_name.split(".")[0])

        soup_user: BeautifulSoup = soup_answer.find("div", {"class": "AuthorInfo"})
        # print(soup_user, "\n*************")
        soup_user_username = soup_user.find("meta", {"itemprop": "name"})
        # print(soup_user_username)
        zhihu_user.username = soup_user_username["content"]
        soup_user_image = soup_user.find("meta", {"itemprop": "image"})
        # print(soup_user_image)
        zhihu_user.image_url = soup_user_image.get("content")
        soup_user_introduction = soup_user.find("div", {"class": "ztext AuthorInfo-badgeText css-14ur8a8"})
        if soup_user_introduction is None:
            zhihu_user.introduction = ""
        else:
            zhihu_user.introduction = soup_user_introduction.text

        zhihu_user_list.append(zhihu_user)

        # print(soup_answer, "\n*************")
        zhihu_answer = ZhihuAnswer()
        zhihu_answer.answer_url = answer_url
        zhihu_answer.zhihu_answer_id = answer_id
        zhihu_answer.id = answer_id
        zhihu_answer.answer_url = answer_url
        soup_answer_content = soup_answer.find("div", {"class": "RichContent-inner"})
        zhihu_answer.content = soup_answer_content
        # print(soup_answer_content)
        zhihu_answer.plain_text = beautify_text(soup_answer_content.get_text())
        zhihu_answer.display_text = zhihu_answer.plain_text[0:100]
        zhihu_answer_list.append(zhihu_answer)

    return shane_question, zhihu_user_list, zhihu_answer_list


def extract_datas():
    file_name_list = os.listdir("other_files/question_title_htmls/question_htmls")
    question_id_list = get_question_id_list()
    for index, file_name in enumerate(file_name_list):
        print(f"第{index}个，id为{file_name}，总共有{len(file_name_list)}个")
        if int(file_name.split(".")[0]) not in question_id_list:
            print("已跳过")
            continue
        try:
            # extract_one_data(file_name)
            shane_question: ShaneQuestion
            zhihu_user_list: list[ZhihuUser]
            zhihu_answer_list: list[ZhihuAnswer]
            shane_question,zhihu_user_list,zhihu_answer_list = extract_one_data(file_name)
            save_in_mysql(shane_question,zhihu_user_list,zhihu_answer_list)
        except:
            print("大失败")
            traceback.print_exc()
            continue
        # break


def save_in_mysql(shane_question: ShaneQuestion, zhihu_user_list: list[ZhihuUser],
                  zhihu_answer_list: list[ZhihuAnswer]):
    conn, cursor = mysql_connect.get_conn()
    try:
        sql_update_question = """
            update shane_question
            set 
                title = %s,
                content = %s,
                display_text = %s,
                plain_text = %s,
                user_id = 0,
                status = 1,
                zhihu_question_id = %s
            where
                id = %s
        """
        cursor.execute(sql_update_question, (
            shane_question.title, shane_question.content, shane_question.display_text, shane_question.plain_text,
            shane_question.id, shane_question.id))

        sql_insert_users = """
            insert into zhihu_user 
                (id,username,introduction,image_url,zhihu_question_id,zhihu_answer_id)
            values 
                (%s, %s, %s, %s, %s, %s)
        """
        insert_user_list = [(generator.generate_id(), user.username, user.introduction, user.image_url,
                             user.zhihu_question_id, user.zhihu_answer_id) for user in zhihu_user_list]
        cursor.executemany(sql_insert_users, insert_user_list)

        sql_insert_answers = """
            insert into zhihu_answer
                (id, question_id, content, plain_text, display_text, answer_url, zhihu_answer_id)
            values 
                (%s, %s, %s, %s, %s, %s, %s)
        """
        insert_answer_list = [(answer.id, answer.question_id, answer.content, answer.plain_text, answer.display_text,
                               answer.answer_url, answer.zhihu_answer_id) for answer in zhihu_answer_list]
        cursor.executemany(sql_insert_answers,insert_answer_list)

        conn.commit()
    except:
        print("操作数据库失败了！！！！！！")
        traceback.print_exc()
    finally:
        cursor.close()
        conn.close()


def beautify_text(text: str):
    # 去除多余的空白字符和换行符
    text = re.sub('\s+', ' ', text)

    # 将文本按段落划分
    paragraphs = text.split('\n\n')

    # 重新格式化文本的缩进
    formatted_text = ''
    indent = ' ' * 4
    for paragraph in paragraphs:
        paragraph = indent + paragraph.strip()
        formatted_text += paragraph + '\n\n'

    return formatted_text.strip()


if __name__ == "__main__":
    extract_datas()
